package com.lmm.lcbusiness.lcbusiness.pages.rnpages.plugins;

import android.net.Uri;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.react.bridge.Callback;
import com.lmm.dresswisdom.lmmframe.reactnative.BasePlugin;
import com.lmm.dresswisdom.lmmframe.reactnative.annotation.RNPlugin;
import com.lmm.lcbusiness.lcbusiness.pages.rnpages.ReactNativeActivity;

import java.util.Map;

/**
 * @author xmgong
 * @date 2017/12/28
 */
@RNPlugin(method = "navi")
public class NaviPlugin extends BasePlugin<ReactNativeActivity> {
    public NaviPlugin(ReactNativeActivity act) {
        super(act);
    }

    @Override
    protected void doAction(Map<String, Object> map, Callback callback) {
        boolean isNeedLogin = (boolean) map.get("isneedLogin");
        final Uri route = Uri.parse(map.get("router").toString());
        Map<String, Object> params = (Map<String, Object>) map.get("params");
        Object value = null;
        Postcard postcard = null;
//        if (isNeedLogin && !AppInfoAction.isLogin()) {
//            postcard = ARouter.getInstance().build("/app/login");
//        } else {
        postcard = ARouter.getInstance().build(route);
//        }
        for (String in : params.keySet()) {
            value = params.get(in);
            if (value instanceof Integer) {
                postcard.withInt(in, Integer.parseInt(String.valueOf(value)));
            }
            if (value instanceof Double) {
                postcard.withDouble(in, Double.parseDouble(String.valueOf(value)));
            }
            if (value instanceof String) {
                postcard.withString(in, String.valueOf(value));
            }
            if (value instanceof Boolean) {
                postcard.withBoolean(in, Boolean.valueOf(String.valueOf(value)));
            }
        }

        postcard.navigation(activity, new NavigationCallback() {
            @Override
            public void onFound(Postcard postcard) {

            }

            @Override
            public void onLost(Postcard postcard) {
                if (route.toString().startsWith("http")) {
                    ARouter.getInstance()
                            .build("/app/webview")
                            .withString("weburl", route.toString()).navigation();
                }
            }

            @Override
            public void onArrival(Postcard postcard) {

            }

            @Override
            public void onInterrupt(Postcard postcard) {

            }
        });
    }
}
